/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utn.tsb.tej.comunications.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Server implements Runnable {

    private static final int PORT = 8181;
    private static final int TIMEOUT = 500;
    private static boolean waitForPlayers;

    
    @Override
    public void run() {

        waitForPlayers = true; //Set the initial value

        try {
            ServerSocket serverSocket; //Socket that will listening the port for players
            serverSocket = new ServerSocket(PORT, TIMEOUT);

            System.out.println("Waiting for players on port " + PORT);
            while (waitForPlayers) {
                Socket newPlayer = serverSocket.accept(); //Get a new player request
                //Runs the player request on a new thread and keep wait for players
                //System.out.println("New Player Ask For Infomation.");
                new Thread(new NewPlayerRequest(newPlayer)).start();
            }
            System.out.println("No more players");
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

}
